package com.moon.pressuretoy;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.IntegerRes;
import androidx.annotation.LayoutRes;
import androidx.appcompat.app.AppCompatActivity;

import com.airbnb.lottie.LottieAnimationView;

public class MainActivity extends AppCompatActivity {

    ImageView ivFinger;
    PowerManager powerManager;
    PowerManager.WakeLock wakeLock;
    LottieAnimationView lottie;
    ImageView iv_sleep;

    FrameLayout fl_touch;

    TextView tv_tip;

    int smallIconSize = 70;
    int bigIconSize = 110;

    int time = 0;

    int interval = 2; //按几次切换
    int maxNum = 6;  //资源最大数
    int maxTimes = 100;   //最大可玩数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ivFinger = findViewById(R.id.ivFinger);
        lottie = findViewById(R.id.lottie);
        tv_tip = findViewById(R.id.tv_tip);
        iv_sleep = findViewById(R.id.iv_sleep);
        fl_touch = findViewById(R.id.fl_touch);


        powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());

        fl_touch.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        start();
                        break;
                    case MotionEvent.ACTION_UP:
                        stop();
                        break;
                }
                return false;
            }
        });
        findViewById(R.id.fl_top).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        setNoTouchable(R.id.fl_right);
        setNoTouchable(R.id.fl_top);
        setNoTouchable(R.id.fl_bottom);
        setNoTouchable(R.id.fl_left);
    }

    private void setNoTouchable(@IdRes int id){
        findViewById(id).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }

    private void stop() {
        if (time > maxTimes)
            return;
        lottie.cancelAnimation();
        lottie.setVisibility(View.INVISIBLE);
        changeWH(smallIconSize);
        ivFinger.setImageResource(R.mipmap.ic_finger);
    }

    private void start() {
        time++;
        if (time > maxTimes) {
            ivFinger.setImageResource(R.mipmap.ic_finger4);
            iv_sleep.setVisibility(View.VISIBLE);
            tv_tip.setText("宝宝，下次再来玩哈");
            return;
        }

        String fileName = getFileName(time);
        lottie.setAnimation(fileName);
        lottie.playAnimation();
        changeWH(bigIconSize);
        lottie.setVisibility(View.VISIBLE);
        ivFinger.setImageResource(R.mipmap.ic_finger3);
    }

    private void changeWH(int dp) {
        ViewGroup.LayoutParams layoutParams = ivFinger.getLayoutParams();
        layoutParams.height = (int) dpToPixel(this, dp);
        layoutParams.width = (int) dpToPixel(this, dp);
        ivFinger.setLayoutParams(layoutParams);
    }


    @Override
    protected void onResume() {
        super.onResume();
        hideBottomUIMenu();

        if (wakeLock != null)
            wakeLock.acquire();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (wakeLock != null)
            wakeLock.release();
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }

    }


    private String getFileName(int time) {
        int i = time / interval;
        int i1 = i % maxNum;
        tv_tip.setText(time + "|" + i1);
        return "star" + i1 + ".json";
    }


    /**
     * dp转px
     *
     * @param context
     * @param dp
     * @return
     */
    public static float dpToPixel(Context context, float dp) {
        return dp * (getDisplayMetrics(context).densityDpi / 160F);
    }

    public static DisplayMetrics getDisplayMetrics(Context context) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        ((WindowManager) context.getSystemService(
                Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(
                displaymetrics);
        return displaymetrics;
    }
}
